﻿(function(){
	window.initWrapper = function (app, position, size) {
		var layer = new Kinetic.Layer();
		layer.add(createBackground(size));
		layer.add(createAddMoneyButton(app, size));
		layer.add(createPurchasingsButton(app, size.height));
		layer.add(createFriendsTopLayer(app, size));
		layer.add(createInviteButton(size.width));
		layer.add(createAchievementsButton(app, size));
		layer.add(createHelpButton(app));
		// title
		layer.add(createText(250, 0, 40, 'Найди деньгу!'));
		// level indicator
		layer.add(createButton(10, 10, 140, 40, 'Уровень: ' + app.currentUser.level));
		// sound
		layer.add(createButton(700, 10, 70, 40, 'Звук'));
        var stage = new Kinetic.Stage({
            container: 'wrapper',
            x: position.x,
            y: position.y,
            width: position.x + size.width,
            height: position.y + size.height
        });
        stage.add(layer);
        return stage;
	};

	function createBackground (stageSize) {
		return new Kinetic.Rect({
			x: 0,
			y: 0,
			fill: 'lightskyblue',
			width: stageSize.width,
			height: stageSize.height,
		});
	}
	function createAddMoneyButton (app, size) {
		var boxHeight = 40;
		var y = size.height - boxHeight - 10;
		var button = createButton(10, y, 80, boxHeight, '$: ' + app.currentUser.money);
		button.on('click', function () {
			app.moveToTop('addMoneyPage');
		});
		return button;
	}
	function createPurchasingsButton (app, stageHeight) {
		var x = 100;
		var boxHeight = 40;
		var y = stageHeight - boxHeight - 10;
		var button = createButton(x, y, 100, boxHeight, 'Покупки!');
		button.on('click', function() {
			app.moveToTop('purchasingsPage');
		});
		return button;
		var rect = createRect(x, y, 100, boxHeight)
		var text = createText(x, y, 20, 'Покупки!');
		layer.add(rect);
		layer.add(text);
	}
	function createFriendsTopLayer (app, size) {
		var gameFriends = app.currentUser.getGameFriends();
		var i = 0;
		var g = new Kinetic.Group();
		for (var friend in gameFriends) {
			g.add(createFriendBox(size.width, gameFriends[friend], i));
			i++;
		};
		return g;
	}
	function createFriendBox (stageWidth, friend, index) {
		var boxWidth = 120;
		var x = stageWidth - boxWidth - 10;
		var boxHeight = 120;
		var y = 160 + (boxHeight + 20) * index;
		var box = new Kinetic.Group();
		box.add(createRect(x, y, boxWidth, boxHeight));
		box.add(createText(x, y, fontSize = 10, text = friend.name));
		return box;
	}
	function createInviteButton (stageWidth) {
		var boxWidth = 120;
		var x = stageWidth - boxWidth - 10;
		var boxHeight = 40;
		var y = 100;
		var button = new Kinetic.Group();
		button.add(createRect(x, y, boxWidth, boxHeight));
		button.add(createText(x, y, 15, 'Позвать друга'));
		return button;
	}
	function createAchievementsButton (app, size) {
		var b = createButton(210, size.height - 50, 130, 40, 'Достижения');
		b.on('click', function () {
			app.moveToTop('achievementsPage');
		});
		return b;
	}
	function createHelpButton (app) {
		var button = createButton(650, 10, 30, 40, '?');
		button.on('click', function () {
			app.moveToTop('helpPage');
		});
		return button;
	}

	function createButton (x, y, width, height, text) {
		var button = new Kinetic.Group();
		button.add(createRect(x, y, width, height));
		button.add(createText(x, y, 20, text));
		button.on('mouseover', function() {
			document.body.style.cursor = 'pointer';
		});
		button.on('mouseout', function() { document.body.style.cursor = 'default'; });
		return button;
	}
	function createRect (x, y, width, height) {
		return window.EngineWrapper.rect(x, y, width, height);
	}
	function createText (x, y, fontSize, text) {
		var el = new Kinetic.Text({
			x: x,
			y: y,
			text: text,
			fontSize: fontSize,
			fill: '#555',
			padding: 10,
			align: 'center'
		});
		return el;
	}
})();